# 19、
# 在17题的基础上，分别将班级1的内容写入到cls1.txt，cls2.txt中，然后再读取打印出来
# 要求：整个过程中若出现异常，需要捕获异常，并将异常信息打印到日志文件中，无异常需打印出：无异常发生
# 不管是否发生异常，均打印出：我进行异常捕获了!
import logging

import openpyxl
from openpyxl.workbook import Workbook
from openpyxl.worksheet.worksheet import Worksheet

from logs.doLog import DoLog
lg=DoLog().getLogger(__name__)

cj1=[50,90,89,70,89,78]
cj2=[40,90,100,70,69,77]

class DoData():
    def dataWrite(self,f,m,s,isUTF=False):
        try:
            if isUTF:
                with open(f,m,encoding='utf-8') as ff:
                    ff.write(s)
            else:
                with open(f,m) as ff:
                    ff.write(s)

        except Exception as e:
            lg.log(logging.ERROR, f"错误：{e}")
        else:
            lg.log(logging.INFO,'无异常')
        finally:
            lg.log(logging.INFO,'我进行异常捕获了!')
            ff.close()
    def writeExcel(self,f,shname,r,c,s):
        try:
            wk: Workbook = openpyxl.load_workbook(f)
            if shname not in wk.sheetnames:
                wk.create_sheet(shname)
            st: Worksheet = wk[shname]
            st.cell(row=r,column=c,value=s)
            b = st.iter_rows(1, r, 1,c,values_only = True)
            l = [i for i in b]
            # l=[[1,2,3],
            #    [4,5,6],
            #    [7,8,9]]
            # for i in l:
            #     st.append(i)
            print(l)
            wk.save(f)
        except Exception as e:
            lg.log(logging.ERROR, f"错误：{e}")
        else:
            lg.log(logging.INFO,'无异常')
        finally:
            lg.log(logging.INFO,'我进行异常捕获了!')



# f1=r'E:\dev\python\project\homeWorkPro\datas\dataHmwk\cls1.txt'
# f2=r'E:\dev\python\project\homeWorkPro\datas\dataHmwk\cls2.txt'
do=DoData()
# s1='50,90,89,70,89,78'
# s2='40,90,100,70,69,77'
# do.dataWrite(f1,'w',s1)
# do.dataWrite(f2,'w',s2)




# 20、
# 创建工作簿wExcel，在do_page的data_page下
# 在writeExcel中新增一个sheet，名称wtest
# 在wtest中，分别输入：
# mes1 mes2               mes3
# 你好   helloword       12
# a.b.c   hellopython   1751323
# 读取writeExcel中的wtest的内容，并打印
# 要求：整个过程中若出现异常，需要捕获异常，并将异常信息打印到日志文件中，无异常需打印出：无异常发生
# 不管是否发生异常，均打印出：我进行异常捕获了!
f=r'E:\dev\python\project\homeWorkPro\datas\dataHmwk\wExecl.xlsx'
# s1='mes1'
# do.writeExcel(f,'wtest',1,1,'mes1')
# s2='mes2'
# do.writeExcel(f,'wtest',1,2,'mes2')
# s3='mes3'
# do.writeExcel(f,'wtest',1,3,'mes3')
# s4='你好'
# do.writeExcel(f,'wtest',2,1,'你好')
# s5='helloword'
# do.writeExcel(f,'wtest',2,2,'helloword')
# s6='12'
# do.writeExcel(f,'wtest',2,3,'12')
# s7='a.b.c'
# do.writeExcel(f,'wtest',3,1,'a.b.c')
# s8='hellopython'
# do.writeExcel(f,'wtest',3,2,'hellopython')
# s9='1751323'
do.writeExcel(f,'wtest',3,3,'1751325')